package com.example.demo5datajpa.domain;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 作用说明：
 *
 * @create 2017-08-29 0:30
 **/
public interface UserRepository extends JpaRepository<User, Integer> {
    User findByName(String name);

    User findByNameAndAge(String name, Integer age);

    List<User> findByAgeOrSalary(Integer age, BigDecimal salary);

    List<User> findByAge(Integer age);

    List<User> findByAgeBetweenAndName(Integer start,Integer end,String name);

    List<User> findBySalaryLessThan(BigDecimal salary);

    List<User> findBySalaryLessThanEqual(BigDecimal salary);

    List<User> findBySalaryGreaterThan(BigDecimal salary);
    List<User> findBySalaryGreaterThanEqual(BigDecimal salary);

    List<User> findBySalaryAfter(BigDecimal salary);

    List<User> findBySalaryBefore(BigDecimal salary);

    List<User> findByNameIsNull();

    List<User> findByNameIsNotNull();

    List<User> findByAgeIsNot(Integer age);

    List<User> findByNameLike(String name);

    List<User> findByNameNotLike(String name);

    List<User> findByNameStartingWith(String name);

    List<User> findByNameStartsWith(String name);

    List<User> findByNameEndingWith(String name);

    List<User> findByNameEndsWith(String name);


    List<User> findByNameContaining(String name);

    List<User> findByNameContains(String name);

    List<User> findByOrderBySalary();
    List<User> findByOrderBySalaryDesc();
    List<User> findByOrderBySalaryAsc();

    List<User> findByIdIn(List<Integer> ids);
    List<User> findByIdNotIn(List<Integer> ids);


    @Query("from User u where u.name=:name")
    User findUser(@Param("name") String name);

    @Transactional
    @Modifying
    @Query("update User u set  u.age=:age where u.name=:name")
    int setNameById(@Param("name") String name, @Param("age") Integer age);

}
